<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Http\Requests\Mini\Meter\ElectricMeter;
use App\Http\Resources\Estate\ElectricMeterResource\ElectricMeterCollection;
use App\Models\ElectricAlarmPlan;
use App\Models\ElectricChargingScheme;
use App\Models\ElectricCollector;
use App\Models\ElectricCollectorCate;
use App\Models\ElectricCommunity;
use App\Models\ElectricElectricity;
use App\Models\ElectricEquipmentLevel;
use App\Services\ElectricMeterService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ElectricMeterController extends Controller
{
    /**
     * Display a listing of the resource.
     *电表设备管理
     * @param Request $request
     * @param ElectricMeterService $electricity_service
     * @return array|Response
     */
    public function index(Request $request, ElectricMeterService $electricity_service)
    {
        $rs = $electricity_service->get_list();
        return $rs['status'] ? $this->success(new ElectricMeterCollection($rs['data'])) : $this->error($rs['msg']);
    }

    /**
     * 2021/3/17--18:38
     * @effect:添加电表
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:store
     * @param ElectricMeter $request
     * @param ElectricMeterService $meter_service
     * @return array
     */
    public function store(ElectricMeter $request, ElectricMeterService $meter_service): array
    {
        $result = $meter_service->add($request);
        return $result['status'] ? $this->success([], __('base.success')) : $this->error($result['msg']);
    }

    /**
     * Display the specified resource.
     *
     * @param ElectricElectricity $meter_model
     * @param int $id
     * @return array|Response
     */
    public function show(ElectricElectricity $meter_model, $id)
    {
        $info = $meter_model->find($id);
        return $this->success($info);
    }

    /**
     * 2021/3/17--18:38
     * @effect:电表设备更新
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:update
     * @param Request $request
     * @param ElectricMeterService $meter_service
     * @return array
     */
    public function update(Request $request,ElectricMeterService $meter_service): array
    {


        $result = $meter_service->edit($request);
        return $result['status'] ? $this->success([], __('base.success')) : $this->error($result['msg']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return Response
     */
    public function destroy(ElectricElectricity $meter_model, $id)
    {
        $idArray = array_filter(explode(',', $id), function ($item) {
            return is_numeric($item);
        });
        $meter_model->destroy($idArray);
        return $this->success([], __('base.success'));
    }

    /**
     * 2021/3/16--13:36
     * @effect:获取数组
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:get_need_arr
     */
    public function get_community($type = 1)
    {
        $community_arr = ElectricCommunity::selectRaw('id value,name label')->where(['status' => 1])->get();//区域数组
        $collector_arr = ElectricCollector::selectRaw('id value,name label')->where(['status' => 1, "type" => $type])->get();//采集器数组
        $alarm_arr = ElectricAlarmPlan::selectRaw('id value,plan_title label')->where(['status' => 1, "alarm_type" => $type])->get();//报警计划数组
        $plan_arr = ElectricChargingScheme::selectRaw('id value,plan_title label')->where(['status' => 1, 'fee_type' => $type])->get();//缴费计划数组
        if ($type == 1) {
            $level_arr = ElectricEquipmentLevel::selectRaw('id value,level_name label')->where(['status' => 1])->get();//设备层级数组
            $electric_arr = ElectricElectricity::selectRaw('id value,name label,level')->where(['status' => 1])->get();
        } else {
            $level_arr = [];
            $electric_arr = [];
        }

        $brand_arr = ElectricCollectorCate::selectRaw("id value,name label")->where(['status' => 1, "type" => $type])->get();//品牌
        return $this->success(compact("collector_arr", "community_arr", "alarm_arr", "plan_arr", "level_arr", "brand_arr", "electric_arr"));
    }

    /**
     * 2021/3/16--13:39
     * @effect:获取父级表
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:get_electric_list
     */
    public function get_electric_list()
    {

//        return $this->success($lst);
    }

    /**
     * @Notes 获取详情
     * @param $communityId
     * @return array
     * @author ypp
     * @date 2021/3/18
     * @time 14:38
     */
    public function electricDetail($id,ElectricMeterService $electricMeterService)
    {
        $result = $electricMeterService->getElectricDetail($id);
        return $result['status'] ? $this->success($result['data']) : $this->error('获取失败');
    }
    /**
     * @Notes 获取实时数据页面展示
     * @param $communityId
     * @return array
     * @author ypp
     * @date 2021/3/18
     * @time 14:38
     */
     public function electricTimeData($id,$communityId,$tableAddress,$code,ElectricMeterService $electricMeterService)
    {
        $data = $electricMeterService->getTimeData($id,$communityId,$tableAddress,$code);
        return $this->success($data);
    }

    /**
     * @Notes 电表详情数据报表数据
     * @param ElectricMeterService $electricMeterService
     * @return array
     * @author ypp
     * @date 2021/3/19
     * @time 15:56
     */
    public function dataReport(ElectricMeterService $electricMeterService)
    {
        $data = $electricMeterService->getDataReport();
        return $this->success($data);
    }

    /**
     * @Notes 电量图表
     * @param ElectricMeterService $electricMeterService
     * @return array
     * @author ypp
     * @date 2021/3/19
     * @time 15:58
     */
    public function eleReport(ElectricMeterService $electricMeterService){
        $data = $electricMeterService->getElectricReport();
        return $this->success($data);
    }

    /**
     * @Notes 获取实时数据
     * @param ElectricMeterService $electricMeterService
     * @return array
     * @author ypp
     * @date 2021/3/19
     * @time 18:17
     */
    public function getRealTimeData(ElectricMeterService $electricMeterService)
    {
        $data = $electricMeterService->getRealTimeData();
        return $this->success($data);
    }

    public function changeLog(ElectricMeterService $electricMeterService)
    {
        $data = $electricMeterService->changeLog();
        return $this->success($data);
    }
}
